package com.jkoss.wine.system.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.vo.ZtreeBean;
import com.jkoss.wine.system.entity.Permission;
import com.jkoss.wine.system.service.IPermissionService;

/**
 * <p>
 * 权限 前端控制器
 * </p>
 *
 * @author chair
 * @since 2018-02-04
 */
@Controller
@RequestMapping("/system/permission")
public class PermissionController extends BaseController {
	@Autowired
	private IPermissionService iPermissionService;

	/**
	 * 列表
	 * 
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/list")
	@RequiresPermissions("/system/permission/list")
	public String list(ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		// 构建排序条件
		Wrapper wrapper = Condition.create().ne("del", 1).orderBy("pid", true).orderBy("sort", true);
		List<Permission> Permissions = iPermissionService.selectList(wrapper);
		// 把查询回来的实体转成ZtreeBean
		if (!CommonUtil.isBlank(Permissions)) {
			List<ZtreeBean> ztreeBeans = new ArrayList();
			for (Permission Permission : Permissions) {
				ZtreeBean ztreeBean = new ZtreeBean();
				ztreeBean.setId(Permission.getId() + "");
				ztreeBean.setIsParent(CommonUtil.isBlank(Permission.getPid()));
				ztreeBean.setName(Permission.getName());
				ztreeBean.setPId(Permission.getPid());
				ztreeBean.setOpen(true);
				ztreeBeans.add(ztreeBean);
			}
			map.put("ztreeBeans", JSON.toJSONString(ztreeBeans));
		} else {
			map.put("ztreeBeans", "[]");
		}

		return "system/permission/list";
	}

	/**
	 * 去新增
	 * 
	 * @param pid
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toInsert")
	@RequiresPermissions("/system/permission/toInsert")
	public String toInsert(String pid, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		map.put("parent", iPermissionService.selectById(pid));
		return "system/permission/edit";
	}

	@RequestMapping("/insert")
	@RequiresPermissions("/system/permission/toInsert")
	@ResponseBody
	public Object insert(Permission permission, HttpServletRequest request, HttpServletResponse response) {
		permission.setDel(0);
		if (iPermissionService.insert(permission)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 去修改
	 * 
	 * @param id
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toUpdate")
	@RequiresPermissions("/system/permission/toUpdate")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		// 查询权限
		Permission permission = iPermissionService.selectById(id);
		map.put("record", permission);
		map.put("parent", iPermissionService.selectById(permission.getPid()));
		map.put("type", permission.getType());
		return "system/permission/edit";
	}

	@RequestMapping("/update")
	@RequiresPermissions("/system/permission/toUpdate")
	@ResponseBody
	public Object update(Permission permission, HttpServletRequest request, HttpServletResponse response) {
		if (iPermissionService.updateById(permission)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 去删除
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("/system/permission/delete")
	@ResponseBody
	public Object delete(String[] id, HttpServletRequest request, HttpServletResponse response) {
		List<Permission> list = new ArrayList<Permission>();
		// 查询所有子集数据
		for (String string : id) {
			Permission permission = iPermissionService.selectById(string);
			if (!CommonUtil.isBlank(permission)) {
				list.add(permission);
			}
			list = getAllPermission(list, string);
		}
		// 修改为删除数据
		List<Permission> dellist = new ArrayList<Permission>();
		for (Permission permission : list) {
			if (permission.getDel() == 0) {
				permission.setDel(1);
				dellist.add(permission);
			}
		}
		// for (String string : id) {
		// Permission permission = new Permission();
		// permission.setId(string);
		// // 是否删除：0-未删除、1-已删除、2-不能删除
		// permission.setDel(1);
		// list.add(permission);
		// }
		if (iPermissionService.updateBatchById(dellist)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	// 递归获取所有子级节点
	public List getAllPermission(List<Permission> list, String id) {
		Wrapper wrapper = Condition.create().eq("pid", id);
		List<Permission> permissions = iPermissionService.selectList(wrapper);
		if (!CommonUtil.isBlank(permissions)) {
			list.addAll(permissions);
		}
		for (Permission permission : permissions) {
			getAllPermission(list, permission.getId());
		}
		return list;
	}
}
